import 'package:flutter/material.dart';
import 'package:flutter_study/flutter/_14%E5%9B%BD%E9%99%85%E5%8C%96/i18n/text_localizations.dart';
import 'package:flutter_study/flutter/_14%E5%9B%BD%E9%99%85%E5%8C%96/i18n/text_localizations_delegate.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

void main() => runApp(MyApp());

/*
  1. pubspec.yaml中配置 flutter_localizations:sdk: flutter
  2. 在iOS中需要配置info.plist文件：新增Localizations添加English、Chinese的支持
  3. AS Flutter Intl插件，可以生成arb文件,每次修改arb文件时使用Ctrl+s重新生成相关文件(.dart、新增的国际化文本)
      arb文件可以传参数 sayHello:hello {name},此时会自动生成一个函数
 */
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '国际化',
      // 声明MaterialApp支持的语言，默认只支持英文
      // 当使用Flutter Intl插件时：S.delegate.supportedLocales
      supportedLocales: [
        const Locale('zh', 'CN'),
        const Locale('en', 'US'),
      ],
      // 让系统widget支持国际化，系统widget的文本
      localizationsDelegates: [
        // 作用是给系统控件，提供国际化语言的值
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        TextLocalizationDelegate.delegate,
        // 当使用Flutter Intl插件时添加 S.deletate：
      ],
      theme: ThemeData(
        primarySwatch: Colors.blue,
        splashColor: Colors.transparent,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('国际化'),
        ),
        body: HomePage(),
      ),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
            // 当使用Flutter Intl插件时:S.of(context).hello,都是自动生成的
            TextLocalization.of(context).hello,
            style: Theme.of(context).textTheme.headline4,
          ),
          ElevatedButton(
            onPressed: () {
              showDatePicker(
                context: context,
                initialDate: DateTime.now(),
                firstDate: DateTime(2000),
                lastDate: DateTime(3000),
                // 强制显示中文
                // locale: Locale('zh')
              );
            },
            child: Text(
              TextLocalization.of(context).pickTime,
              style: Theme.of(context).textTheme.headline4,
            ),
          )
        ],
      ),
    );
  }
}
